import math
import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar
import qrcode
from PIL import Image
import time
from docx import Document

from docx.shared import Inches

doc = Document()  # doc对象


def welcome():
    print("请输入要生成二维码的行数：")
    row = input()
    print("请输入要生成二维码的列数：")
    column = input()
    print("两个二维码相隔的距离：")
    distance = input()
    for i in range(0, int(row), int(distance)):
        for j in range(0, int(column), int(distance)):
            text = 'x=' + str(i) + ',' + 'y=' + str(j)
            print(text)
            # 生成二维码
            img = qrcode.make(data=text)
            # # 直接显示二维码
            # img.show()
            # 保存二维码为文件
            # img=cv2.resize(1024,1024)
            img.save("qrcode.jpg")

            # 加载背景图片
            bk_img = cv2.imread("qrcode.jpg")
            # 在图片上添加文字信息
            bk_img = cv2.resize(bk_img, (480, 480), 0, 0)

            shape_y = bk_img.shape[0]
            shape_x = bk_img.shape[1]

            # 逆时针旋转90度，算法工程师太懒了，吐槽死他
            matRotate = cv2.getRotationMatrix2D((shape_y * 0.5, shape_x * 0.5), 90, 1)  # 旋转变化矩阵
            bk_img = cv2.warpAffine(bk_img, matRotate, (shape_x, shape_y))  # 旋转

            # 去除黑边
            bk_img = bk_img[5:480, 5:480]
            bk_img = cv2.resize(bk_img, (480, 480), 0, 0)

            texta = "Mobot    " + text
            cv2.putText(bk_img, texta, (int(shape_y / 2) - 80, 40), cv2.FONT_HERSHEY_SIMPLEX,
                        0.5, (218, 123, 233), 2, cv2.LINE_AA)

            ptStart = (int(shape_y / 2), 0)
            ptEnd = (int(shape_y / 2), 20)
            point_color = (0, 0, 0)  # BGR
            thickness = 3
            lineType = 4
            cv2.line(bk_img, ptStart, ptEnd, point_color, thickness, lineType)

            ptStart = (int(shape_y / 2), int(shape_x) - 20)
            ptEnd = (int(shape_y / 2), int(shape_x))
            point_color = (0, 0, 0)  # BGR
            thickness = 3
            lineType = 4
            cv2.line(bk_img, ptStart, ptEnd, point_color, thickness, lineType)

            ptStart = (0, int(shape_x / 2))
            ptEnd = (20, int(shape_x / 2))
            point_color = (0, 0, 0)  # BGR
            thickness = 3
            lineType = 4
            cv2.line(bk_img, ptStart, ptEnd, point_color, thickness, lineType)

            ptStart = (int(shape_y) - 20, int(shape_x / 2))
            ptEnd = (int(shape_y), int(shape_x / 2))
            point_color = (0, 0, 0)  # BGR
            thickness = 3
            lineType = 4
            cv2.line(bk_img, ptStart, ptEnd, point_color, thickness, lineType)

            # 保存图片
            cv2.imwrite("%d_%d.jpg" % (i, j), bk_img)
            icon = Image.open('2.jpg')

            # 保存图片
            # cv2.imshow('x5y5.jpg', bk_img)
            bk_img = Image.open("%d_%d.jpg" % (i, j))
            assert isinstance(bk_img, Image.Image)
            # 参数设置logo的大小
            factor = 6
            size_w = int(shape_y / factor)
            size_h = int(shape_x / factor)
            icon_w, icon_h = icon.size
            if icon_w > size_w:
                icon_w = size_w
            if icon_h > size_h:
                icon_h = size_h
            # 重新设置logo的尺寸
            icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)

            # 得到画图的x，y坐标，居中显示
            w = int((shape_y - icon_w) / 2)
            h = int((shape_x - icon_h) / 2)
            # 黏贴logo照
            bk_img.paste(icon, (w, h), mask=None)
            bk_img.save("%d_%d.jpg" % (i, j))

            doc.add_paragraph("                                     ")  # 添加文字
            doc.add_picture("%d_%d.jpg" % (i, j), width=Inches(2.00))

    doc.save('1.docx')


if __name__ == '__main__':
    welcome()
